package com.kystar.muxplayer.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.kystar.muxplayer.R;

/**
 * Seekbar
 *
 * Created by zhusheng on 2018-02-09.
 */

public class ColorSeekBar extends LinearLayout {

    public interface OnChangeListener {
        void OnChange(int value);
    }

    View contentView;
    SeekBar seekBar;
    TextView indicator;
    OnChangeListener changeListener;
    int lastProgress = Integer.MAX_VALUE;

    public ColorSeekBar(Context context) {
        super(context);
        setup();
    }

    public ColorSeekBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setup();
    }

    public void setOnChangListener(OnChangeListener listener) {
        changeListener = listener;
    }

    public int getValue() {
        return seekBar.getProgress();
    }

    public void setValue(int value) {
        seekBar.setProgress(value);
    }

    public int getMax() {
        return seekBar.getMax();
    }

    public void setMax(int value) {
        seekBar.setMax(value);
    }

    public void setup() {
        int padding = (int)(getContext().getResources().
                getDimension(R.dimen.main_setting_seekbar_padding) + 0.5);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        contentView = inflater.inflate(R.layout.layout_seekbar, null);
        addView(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        indicator = contentView.findViewById(R.id.seekbar_indicator);
        seekBar = contentView.findViewById(R.id.seekbar);
        seekBar.setPadding(padding,0,padding,0);
        seekBar.setIndeterminate(false);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                syncIndicator(seekBar, indicator, progress, 0, false);
                if (changeListener != null) {
                    changeListener.OnChange(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        contentView.findViewById(R.id.seekbar_left).setOnClickListener(v -> {
            int value = seekBar.getProgress();
            if (value > 0) {
                seekBar.setProgress(value-1);
            }
        });

        contentView.findViewById(R.id.seekbar_right).setOnClickListener(v -> {
            int value = seekBar.getProgress();
            if (value < seekBar.getMax()) {
                seekBar.setProgress(value+1);
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        syncIndicator(true);
    }

    public void syncIndicator(boolean force) {
        syncIndicator(seekBar, indicator, seekBar.getProgress(), 0, force);
    }

    private void syncIndicator(SeekBar seekBar, TextView indicator, int progress, int min, boolean force) {
       // log.debug("syncIndicator", lastProgress, progress, ColorSeekBar.this.getTag(), force);

        if (!force && lastProgress == progress ) {
            return;
        }
        lastProgress = progress;

        indicator.setText(String.valueOf(progress+min));
        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        indicator.measure(spec, spec);

        int indWidth = indicator.getMeasuredWidth();
        int sbWidth = seekBar.getMeasuredWidth();
        int sbPaddingL = seekBar.getPaddingLeft();
        int sbPaddingR = seekBar.getPaddingRight();
        double margin  = ((LayoutParams)seekBar.getLayoutParams()).leftMargin;
        margin += seekBar.getPaddingLeft();
        margin += ((double) progress / seekBar.getMax()) * (sbWidth - sbPaddingL - sbPaddingR);
        margin -= indWidth / 2;
        margin += 0.5;

        LayoutParams params = (LayoutParams) indicator.getLayoutParams();
        params.leftMargin = (int)margin;
        indicator.setLayoutParams(params);
    }
}
